import { useMutation, useQuery, useQueryClient } from '@umijs/max';
import { request } from '../axios';
import QUERY_KEYS from '../constant';
import APP_PRE from '../constant/app-pre';

const prefix = `${APP_PRE.APP_NAME}/action`;

//获取账号对文章的行为
const useGetArticleAction = (accountId: number, articleId: number) => {
  return useQuery([QUERY_KEYS.ARTICLE_ACTION_GET, accountId, articleId], () =>
    request<API.IResult<API.IArticleAction[]>>(`${prefix}/get`, {
      method: 'GET',
      params: {
        accountId,
        articleId,
      },
    }),
  );
};

//获取账号的所有收藏文章
const useGetAllCollection = (accountId: number) => {
  return useQuery(
    [QUERY_KEYS.ARTICLE_ACTION_GET_ALL_COLLECTION, accountId],
    () =>
      request<API.IResult<API.IArticles[]>>(`${prefix}/getAllCollection`, {
        method: 'GET',
        params: {
          accountId,
        },
      }),
  );
};

// 修改
const useUpdateArticleAction = (key?: string) => {
  const query = useQueryClient();
  return useMutation(
    (articleAction: API.IArticleAction) =>
      request<API.IResult<API.IInsertId>>(`${prefix}/update`, {
        method: 'POST',
        data: articleAction,
      }),
    {
      onSuccess(res: API.IResult<API.IInsertId>) {
        if (res.code === 200) {
          console.log('ss');
          query.invalidateQueries([key]);
        }
      },
    },
  );
};

export { useGetArticleAction, useUpdateArticleAction, useGetAllCollection };
